package com.example.batchdemo.util;

import lombok.SneakyThrows;

import java.sql.*;
import java.util.Properties;

/**
 * TODO    测试 requesDB
 *
 * @ClassName ConnectExample
 * @Author xiaojiang
 * @Date 2021/12/28 下午 14:33
 */
public class ConnectExample {

    @SneakyThrows
    public static void main(String[] args) throws SQLException {


        Properties properties = new Properties();
        properties.setProperty("user", "admin");
        properties.setProperty("password", "quest");
        properties.setProperty("sslmode", "disable");
        System.out.println("---开始---");
        final Connection connection = DriverManager.getConnection("jdbc:postgresql://192.168.3.14:8812/", properties);
        System.out.println("---已连接---");

        //connection.setAutoCommit(false);
        // 如果表不存在则创建表
        //final PreparedStatement statement = connection.prepareStatement("CREATE TABLE IF NOT EXISTS user ( username STRING, address STRING,password STRING);");
        //statement.execute();

        // 插入数据
        try (PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO user (username,address,password)  VALUES (?, ?, ?)")) {
            preparedStatement.setString(1, "eryeryt");
            preparedStatement.setString(2, "sahnghai");
            preparedStatement.setString(3, "8dfsdge");
            //preparedStatement.setInt(2, 123);
            boolean a = preparedStatement.execute();
            System.out.println("=====" + a);
        }
        System.out.println("Done");

        // 查询数据
        try (PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM user;")) {
            try (ResultSet rs = preparedStatement.executeQuery()) {
                while (rs.next()) {
                    System.out.println(rs);
                    System.out.println(rs.getString("username"));
                    System.out.println(rs.getString("address"));
                    //System.out.println(rs.getLong(1));
                }
            }
        }
        connection.close();

    }




}
